<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" width="50%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="模板名称" prop="name">
        <el-input placeholder="输入模板名称" v-model="dataForm.name" maxlength="30" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="模板类型" prop="free_post">
        <el-radio-group v-model="dataForm.free_post" @change="freePost">
          <el-radio :label="false">买家承担运费</el-radio>
          <el-radio :label="true">卖家包邮</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="计价方式" prop="freight_compute_mode" v-if="dataForm.free_post ==false">
        <el-radio-group v-model="dataForm.freight_compute_mode">
          <el-radio :label="0">按件数</el-radio>
          <el-radio :label="1">按重量</el-radio>
          <el-radio :label="2">按体积</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="默认模板">
        <el-switch v-model="dataForm.general" active-color="#ff4949" inactive-color="#cccccc">
        </el-switch>
        <span>设为新商品默认模板（新添加的商品支持商家配送时的默认模板）</span>
      </el-form-item>
      <table style="width: 100%" v-if="dataForm.free_post ==false && dataForm.freight_compute_mode !=null ">
        <tr>
          <th style="width: 20%;">可配送区域</th>
          <th v-if="dataForm.freight_compute_mode==0">首件数（个）</th>
          <th v-if="dataForm.freight_compute_mode==1">首重量（Kg）</th>
          <th v-if="dataForm.freight_compute_mode==2">首体积 （M3）</th>
          <th>运费（元）</th>
          <th v-if="dataForm.freight_compute_mode==0">续件（个）</th>
          <th v-if="dataForm.freight_compute_mode==1">续重量（Kg）</th>
          <th v-if="dataForm.freight_compute_mode==2">续体积（M3）</th>
          <th>续费（元）</th>
        </tr>
        <tr>
          <th>默认区域</th>
          <th v-if="dataForm.freight_compute_mode!=null">
            <el-form-item>
              <el-input v-model="dataForm.default_rule.start_standards"></el-input>
            </el-form-item>
          </th>
          <th>
            <el-form-item>
              <el-input v-model="dataForm.default_rule.start_freight"></el-input>
            </el-form-item>
          </th>
          <th>
            <el-form-item>
              <el-input v-model="dataForm.default_rule.continued"></el-input>
            </el-form-item>
          </th>
          <th>
            <el-form-item>
              <el-input v-model="dataForm.default_rule.continued_freight"></el-input>
            </el-form-item>
          </th>
        </tr>
      </table>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  treeDataTranslate
} from '@/utils'
import seviceApi from '@/api/serviceApi.js'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        name: '',
        free_post: null, // 是否免邮
        general: false, // 是否是默认模板
        freight_compute_mode: 0, // 运费计算方式(0件，1重,2体积)
        default_rule: {
          start_standards: null, // 首件，首重，首体积初始门槛（首件，首重，首体积(运费)）
          start_freight: null, // 初始运费（首件，首重，首体积(运费)）
          continued: null, // 续加多少量
          continued_freight: null // 续加的运费
        }
      },
      dataRule: {
        name: [{
          required: true,
          message: '模板名称不能为空',
          trigger: 'blur'
        }],
        free_post: [{
          required: true,
          message: '请选择是否免邮',
          trigger: 'blur'
        }],
        general: [{
          required: true,
          message: '请选择是否是默认模板',
          trigger: 'blur'
        }]
      }
    }
  },

  methods: {
    init (id) {
      this.reset()

      this.dataForm.id = id || null
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
      })
      if (id) {
        // 存在id 查询详情
        seviceApi.getFreighttemplateInfo(id).then(res => {
          if (res && res.data.code == 200) {
            this.dataForm = res.data.data
          }
        })
      }
    },
    freePost (e) {
      if (e == false) {
        this.dataForm.freight_compute_mode = 0
      }
    },
    // 提交
    dataFormSubmit () {
      if (this.dataForm.free_post) {
        this.dataForm.freight_compute_mode = 0
        this.dataForm.default_rule.start_standards = 0
        this.dataForm.default_rule.start_freight = 0
        this.dataForm.default_rule.continued = 0
        this.dataForm.default_rule.continued_freight = 0
      }
      if (!this.dataForm.id) {
        delete this.dataForm.id
        seviceApi.addFreighttemplate(this.dataForm).then(res => {
          if (res && res.data.code == 200) {
            this.$message({
              message: '添加成功',
              type: 'success'
            })
            this.visible = false
            this.$emit('result')
          }
        })
      } else {
        seviceApi.updateFreighttemplate(this.dataForm).then(res => {
          if (res && res.data.code == 200) {
            this.$message({
              message: '修改成功',
              type: 'success'
            })
            this.visible = false
            this.$emit('result')
          }
        })
      }
    },

    reset () {
      this.dataForm = {
        name: '',
        free_post: null, // 是否免邮
        general: false, // 是否是默认模板
        freight_compute_mode: null, // 运费计算方式(0件，1重,2体积)
        default_rule: {
          start_standards: null, // 首件，首重，首体积初始门槛（首件，首重，首体积(运费)）
          start_freight: null, // 初始运费（首件，首重，首体积(运费)）
          continued: null, // 续加多少量
          continued_freight: null // 续加的运费
        }
      }
    }

  }
}
</script>

<style lang="scss">
table {
  border-collapse: collapse;
  border-spacing: 0;
  border-left: 1px solid #cccccc;
  border-top: 1px solid #cccccc;
}

th {
  border-right: 1px solid #cccccc;
  border-bottom: 1px solid #cccccc;
  padding: 5px 15px;
}

th .el-form-item {
  margin-bottom: 0px;
}

th .el-form-item__content {
  margin-left: 0px !important;
}
</style>
